package com.xkstream.client.view.gift;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.lgim.LgExtension;
import com.xkstream.client.utils.AsyncImage;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue;


/**
 * @author zhongxf
 * @Description 礼物显示的管理类
 * @Date 2016/6/6.
 * 主要礼物逻辑：利用一个LinkedBlockingQueue来存储所有的礼物的实体类。然后利用Handler的消息机制，每隔一段时间从队列中取一次礼物出来
 * 如果取得礼物为空（队列中没有礼物），那么就延迟一段时间之后再次从队列中取出礼物
 * 如果从队列中取出的礼物不为空，则根据送礼物的人的UserId去寻找这个礼物是否正在显示，如果不在显示，则新建一个，如果正在显示，则直接修改数量
 * <p/>
 * 这个礼物View的管理类中一直存在一个定时器在沦陷礼物的容器下面的所有的礼物的View，当有礼物的View上次的更新时间超过最长显示时间，那么久就移除这个View
 * <p/>
 * 6/7实现：礼物容器中显示的礼物达到两条，并且新获取的礼物和他们两个不一样，那么需要移除一个来显示新的礼物
 * 判断所有的里面的出现的时间，然后把显示最久的先移除掉（需要考虑到线程安全）
 * <p>
 * 6/7实现：定时器的线程会更新View，在获取礼物的时候也会更新View（增加线程安全控制）
 */
public class GiftShowManager_Lg {

    private LinkedBlockingQueue<LgExtension.GiftInfo> queue;//礼物的队列
    private LinearLayout giftCon;//礼物的容器
    private Context cxt;//上下文

    private Animation inAnim;//礼物View出现的动画
    private Animation outAnim;//礼物View消失的动画
    private Animation giftNumAnim;//修改礼物数量的动画

    private final static int SHOW_GIFT_FLAG = 1;//显示礼物
    private final static int GET_QUEUE_GIFT = 0;//从队列中获取礼物
    private final static int REMOVE_GIFT_VIEW = 2;//当礼物的View显示超时，删除礼物View

    private Timer timer;//轮询礼物容器的所有的子View判断是否超过显示的最长时间
    TimerTask task;
    private Map<String,LgExtension.GiftInfo> mapCaches = new LinkedHashMap<>();

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            try {
                switch (msg.what) {
                    case SHOW_GIFT_FLAG://如果是处理显示礼物的消息
                        LgExtension.GiftInfo showVo = (LgExtension.GiftInfo ) msg.obj;

                        String userId = showVo.getSenderCode();
                        int num = showVo.getGift_number();

                        View giftView = giftCon.findViewWithTag(userId + showVo.getGift_name());
                        giftNumAnim.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 300);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {
                            }
                        });

                        if (giftView == null) {//获取的礼物的实体，判断送的人不在显示

                            //首先需要判断下Gift ViewGroup下面的子View是否超过两个
                            int count = giftCon.getChildCount();
                            if (count >= 2) {//如果正在显示的礼物的个数超过两个，那么就移除最后一次更新时间比较长的

                                View giftView1 = giftCon.getChildAt(0);

                                TextView nameTv1 = (TextView) giftView1.findViewById(R.id.name);
                                //long lastTime1 = (long) nameTv1.getTag();
                                int num1 = (int) giftView1.findViewById(R.id.gift_num).getTag();


                                View giftView2 = giftCon.getChildAt(1);
                                TextView nameTv2 = (TextView) giftView2.findViewById(R.id.name);
//                            long lastTime2 = (long) nameTv2.getTag();
                                int num2 = (int) giftView2.findViewById(R.id.gift_num).getTag();
                                Message rmMsg = new Message();
                                if (num1 > num2) {//如果第二个View显示的时间比较长
                                    rmMsg.obj = 1;

                                } else {//如果第一个View显示的时间长
                                    rmMsg.obj = 0;
                                }
                                rmMsg.what = REMOVE_GIFT_VIEW;
                                handler.sendMessage(rmMsg);
                            }


                            //获取礼物的View的布局
                            giftView = LayoutInflater.from(cxt).inflate(R.layout.gift_item, null);
                            giftView.setTag(userId + showVo.getGift_name());
                            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            lp.topMargin = 10;
                            giftView.setLayoutParams(lp);

                            //显示礼物的数量
                            final GiftTextView giftNum = (GiftTextView) giftView.findViewById(R.id.gift_num);
                            giftNum.setTag(num);
                            giftNum.setText("x" + num);
                            ImageView face = (ImageView) giftView.findViewById(R.id.face);
                            AsyncImage.loadNetPhoto(showVo.getGift_logo(), face);

                            TextView tv = (TextView) giftView.findViewById(R.id.name);
                            TextView tvGift = (TextView) giftView.findViewById(R.id.giftName);
//                        face.setImageBitmap(DownloadImage.getInstance().getBitmap("http://img.blog.csdn.net/20151117102003607"));
                            tv.setText(showVo.getSenderName());
                            tv.setTag(System.currentTimeMillis());
                            tvGift.setText(showVo.getGift_name());
                            //将礼物的View添加到礼物的ViewGroup中
                            giftCon.addView(giftView);

                            giftView.startAnimation(inAnim);//播放礼物View出现的动

                            inAnim.setAnimationListener(new Animation.AnimationListener() {
                                @Override
                                public void onAnimationStart(Animation animation) {
                                }

                                @Override
                                public void onAnimationEnd(Animation animation) {
                                    giftNum.startAnimation(giftNumAnim);
                                }

                                @Override
                                public void onAnimationRepeat(Animation animation) {
                                }
                            });
                        } else {//如果送的礼物正在显示（只是修改下数量）
                            //显示礼物的数量
                            final GiftTextView giftNum = (GiftTextView) giftView.findViewById(R.id.gift_num);
                            int lastNum = (int) giftNum.getTag();
                            int showNum = lastNum + 1;
                            giftNum.setTag(showNum);
                            giftNum.setText("x" + (showNum));

                            TextView tv = (TextView) giftView.findViewById(R.id.name);
                            tv.setTag(System.currentTimeMillis());

                            giftNum.startAnimation(giftNumAnim);
                            if (giftCon.getChildCount() == 2) {
                                View c1 = giftCon.getChildAt(0);
                                View c2 = giftCon.getChildAt(1);
                                int num1 = (int) c1.findViewById(R.id.gift_num).getTag();
                                int num2 = (int) c2.findViewById(R.id.gift_num).getTag();
                                if (c1.getY() < c2.getY() && num1 < num2) {
                                    Animator up = ObjectAnimator.ofFloat(c1, "translationY", 0, c1.getHeight());
                                    up.setDuration(300);
                                    up.start();

                                    Animator down = ObjectAnimator.ofFloat(c2, "translationY", 0, -c1.getHeight());
                                    down.setDuration(300);
                                    down.start();
                                } else if (c1.getY() > c2.getY() && num1 > num2) {
                                    Animator up = ObjectAnimator.ofFloat(c1, "translationY", c1.getHeight(), 0);
                                    up.setDuration(300);
                                    up.start();

                                    Animator down = ObjectAnimator.ofFloat(c2, "translationY", -c1.getHeight(), 0);
                                    down.setDuration(300);
                                    down.start();
                                }
                            }

                        }
                        if (giftCon.getChildCount() > 0) {
                            View c = giftCon.getChildAt(0);
                            if (c.getTranslationY() < 0) {
                                c.setTranslationY(0);
                                c.invalidate();
                            }
                        }
                        break;
                    case GET_QUEUE_GIFT://如果是从队列中获取礼物实体的消息
                        LgExtension.GiftInfo vo = queue.poll();
                        if (vo != null) {//如果从队列中获取的礼物不为空，那么就将礼物展示在界面上
                            Message giftMsg = new Message();
                            giftMsg.obj = vo;
                            giftMsg.what = SHOW_GIFT_FLAG;
                            handler.sendMessage(giftMsg);
                        } else {
                            handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 1000);//如果这次从队列中获取的消息是礼物是空的，则一秒之后重新获取
                        }
                        break;

                    case REMOVE_GIFT_VIEW:
                        final int index = (int) msg.obj;
                        Log.e("TAG", "index  " + index);
                        if (index == 0 && giftCon.getChildCount() > 1) {

                        /*
                        Animator down =  ObjectAnimator.ofFloat(c1,"translationY",-c1.getHeight(),0);
                        down.setDuration(10);
                        down.start();*/
                            for (int i = 0; i < giftCon.getChildCount(); i++) {
                                View c = giftCon.getChildAt(i);
                                //        L.e("TAG", "index c2    " + i + "      h     " + c.getTranslationY());
                                if (c.getTranslationY() < 0) {
                                    c.setTranslationY(0);
                                    c.invalidate();
                                }
                            }

                        }
                        final View removeView = giftCon.getChildAt(index);
                        outAnim.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {
                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                new Handler().post(new Runnable() {
                                    @Override
                                    public void run() {
                                        giftCon.removeView(removeView);
                                        if (giftCon.getChildCount() > 0) {
                                            View c = giftCon.getChildAt(0);
                                            //           L.e("TAG","c.getTranslationY()"+c.getTranslationY());
                                            if (c.getTranslationY() < 0) {
                                                c.setTranslationY(0);
                                                c.invalidate();
                                            }
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {
                            }
                        });
                        if (removeView != null)
                            removeView.startAnimation(outAnim);
                        break;
                    case 444:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    public GiftShowManager_Lg(Context cxt, final LinearLayout giftCon) {
        this.cxt = cxt;
        this.giftCon = giftCon;
        queue = new LinkedBlockingQueue<LgExtension.GiftInfo>();
        inAnim = AnimationUtils.loadAnimation(cxt, R.anim.gift_in);
        outAnim = AnimationUtils.loadAnimation(cxt, R.anim.gift_out);
        giftNumAnim = AnimationUtils.loadAnimation(cxt, R.anim.gift_num);

        task = new TimerTask() {
            @Override
            public void run() {
                int count = giftCon.getChildCount();
                int giftNum = 0;
                if (giftCon.getChildCount() > 0) {
                    giftNum = (int) giftCon.getChildAt(0).findViewById(R.id.gift_num).getTag();
                }
                int index = 0;
                for (int i = 0; i < count; i++) {
                    View view = giftCon.getChildAt(i);
                    int num = (int) view.findViewById(R.id.gift_num).getTag();
                    if (giftNum > num) {
                        giftNum = num;
                        index = i;
                    }

                }
                if (giftCon.getChildCount() > 0) {
                    TextView name = (TextView) giftCon.getChildAt(index).findViewById(R.id.name);
                    long nowtime = System.currentTimeMillis();
                    long upTime = (long) name.getTag();
                    if ((nowtime - upTime) >= 5000) {
                        Message msg = new Message();
                        msg.obj = index;
                        msg.what = REMOVE_GIFT_VIEW;
                        handler.sendMessage(msg);
                    }
                }
            }
        };
        timer = new Timer();
        timer.schedule(task, 2000, 2000);
        timer.schedule(giftTask, 200, 500);
    }

    TimerTask giftTask = new TimerTask() {
        @Override
        public void run() {
            if (mapCaches.isEmpty()) {
                Message msg = new Message();
                msg.what = 444;
                handler.sendMessage(msg);
            } else {
                int max = 0;
                LgExtension.GiftInfo popGift = null;
                for (Map.Entry<String, LgExtension.GiftInfo> entry : mapCaches.entrySet()) {
                    LgExtension.GiftInfo g = entry.getValue();
                    if (g.getGift_number() > max) {
                        max = g.getGift_number();
                        popGift = g;
                    }
                }
                if (popGift != null) {
                    mapCaches.remove(popGift.getSenderCode() + popGift.getGift_name());
                    queue.add(popGift);
                }
            }
        }
    };

    //开始显示礼物
    public void showGift() {
        if (handler != null)
            handler.sendEmptyMessageDelayed(GET_QUEUE_GIFT, 1000);//轮询队列获取礼物
    }

    //放入礼物到队列
    public boolean addGift(LgExtension.GiftInfo vo) {
        //return queue.add(vo);
        mapCaches.put(vo.getSenderCode() + vo.getGift_name(), vo);
        return true;
    }

    public void release() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (task != null) {
            task.cancel();
            task = null;
        }
        if (giftTask != null) {
            giftTask.cancel();
            giftTask = null;
        }

        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
